package net.area80.numbers
{
	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.crypto.symmetric.IPad;
	import com.hurlant.crypto.symmetric.PKCS5;
	import com.hurlant.util.Base64;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;

	public class EncodeINT
	{
		private var type:String='simple-des-ecb';
		private var key:ByteArray;
		private var _value:String;
		public function EncodeINT(initValue:Number,key:ByteArray)
		{
			this.key = key;
			value = initValue;
		}
		public function get value ():int {
			return int(decrypt(_value));
		}
		public function set value (v:int):void {
			_value = encrypt(String(v));
		}
		private function encrypt(txt:String = ''):String
		{
			var data:ByteArray = Hex.toArray(Hex.fromString(txt));
			 
			var pad:IPad = new PKCS5;
			var mode:ICipher = Crypto.getCipher(type, key, pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.encrypt(data);
			return Base64.encodeByteArray(data);
		}
		private function decrypt(txt:String = ''):String
		{
			var data:ByteArray = Base64.decodeToByteArray(txt);
			var pad:IPad = new PKCS5;
			var mode:ICipher = Crypto.getCipher(type, key, pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.decrypt(data);
			return Hex.toString(Hex.fromArray(data));
		}
	}
}